﻿document.Cookies={};
Function.registerNameSpace("AGuang");

AGuang.HttpCookie=function(name){
    this._name=name;
}

AGuang.HttpCookie.prototype={
    get_name:function(){
        return this._name;
    },
    set_name:function(value){
        this._name=value;
    },
    get_value:function(){
        return this._value;
    },
    set_value:function(value){
        this._value=value;
        this.clear();
    },
    get_path:function(){
        return this._path;
    },
    set_path:function(value){
        this._path=value;
    },
    get_expires:function(){
        return this._expires;
    },
    set_expires:function(value){
        this._expires=value;
    },
    get_domain:function(){
        return this._domain;
    },
    set_domain:function(value){
        this._domain=value;
    },
    get_secure:function(){
        return this._secure;
    },
    set_secure:function(value){
        this._secure=value;
    },
    add:function(key,value){
        if(!this["1234____keyValues"]) this["1234____keyValues"]={};
        this["1234____keyValues"][key]=value;
    },
    remove:function(key){
        if(!this["1234____keyValues"]) return;
        this["1234____keyValues"][key]=null;
    },
    clear:function(){
        this["1234____keyValues"]=null;
    },
    get:function(name){
        if(!this["1234____keyValues"]) return null;
        return this["1234____keyValues"][name];
    },
    hasKeys:function(){
        if(this["1234____keyValues"]) return true;
        else return false;
    },
    toString:function(){
        return this.get_value();
    }
}

document.Cookies.add=function(httpCookie){
    var cookieValue="";
    if(httpCookie.hasKeys()){
        var i=0;
        var val="";
        for(var k in httpCookie["1234____keyValues"]){
            var v=httpCookie["1234____keyValues"][k];
            if(v){
                if(i>=1) val+="&";
                val+=k+"="+escape(v);
                i++; 
            }
        }
    cookieValue=httpCookie.get_name()+"="+val;
    }else cookieValue=httpCookie.get_name() + "=" + escape(httpCookie.get_value());
    if(httpCookie.get_expires()) cookieValue+="; expires=" + httpCookie.get_expires().toGMTString();
    if(httpCookie.get_path()) cookieValue+="; path=" + httpCookie.get_path();
    if(httpCookie.get_domain()) cookieValue+="; domain=" + httpCookie.get_domain();
    if(httpCookie.get_secure()) cookieValue+="; secure";
    document.cookie=cookieValue;
}

document.Cookies.remove=function(key){
    if(document.Cookies["1234___monery"]) document.Cookies["1234___monery"][key]=null;
    var httpCookie=new AGuang.HttpCookie(key);
    httpCookie.set_value("");
    var expdate = new Date(); 
    expdate.setTime(expdate.getTime() - (86400 * 1000 * 1)); 
    httpCookie.set_expires(expdate);
    document.Cookies.add(httpCookie);
}

document.Cookies.clear=function(){
    document.Cookies["1234___monery"]=null;
    if(document.cookie!=null){
        var groups=document.cookie.split(";");
        for(var i=0;i<groups.length;i++){
            var cValue=groups[i].trim();
            var k,v;
            var contentStart=cValue.indexOf("=");
            if(contentStart<=-1){
                k=cValue;
            }else{
                k=cValue.substr(0,contentStart);
            }
            document.Cookies.remove(k);
        }
    }
}

document.Cookies.get=function(name){
    name=name.toLowerCase();
    if(!document.Cookies["1234___monery"]) document.Cookies["1234___monery"]={};
    if(document.Cookies["1234___monery"][name]) return document.Cookies["1234___monery"][name];
    if(document.cookie!=null){
        var groups=document.cookie.split(";");
        for(var i=0;i<groups.length;i++){
            var cValue=groups[i].trim();
            var k,v;
            var contentStart=cValue.indexOf("=");
            if(contentStart<=-1){
                k=cValue.toLowerCase();
                v="";
            }else{
                k=cValue.substr(0,contentStart).toLowerCase();
                v=cValue.substr(contentStart+1);
            }
            var httpCookie=new AGuang.HttpCookie(k);
            if(v.indexOf("=")>-1){
                var inVals=v.split("&");
                for(var j=0;j<inVals.length;j++){
                    var inVal=inVals[j].trim();
                    var inK,inV;
                    var inVStart=inVal.indexOf("=");
                    if(inVStart<=-1){
                        inK=inVal;
                        inV="";
                    }else{
                        inK=inVal.substr(0,inVStart);
                        inV=inVal.substr(inVStart+1);
                    }
                    httpCookie.add(inK,inV);
                }
            }else httpCookie.set_value(unescape(v));
            document.Cookies["1234___monery"][k]=httpCookie;
        }
    }
    if(document.Cookies["1234___monery"][name]) return document.Cookies["1234___monery"][name];
    else return null;
}